(function (global, factory) {
	typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('babel-runtime/helpers/typeof'), require('babel-runtime/core-js/promise'), require('babel-runtime/core-js/object/assign'), require('babel-runtime/helpers/extends'), require('babel-runtime/core-js/json/stringify'), require('babel-runtime/helpers/objectWithoutProperties'), require('stat/core')) :
	typeof define === 'function' && define.amd ? define(['babel-runtime/helpers/typeof', 'babel-runtime/core-js/promise', 'babel-runtime/core-js/object/assign', 'babel-runtime/helpers/extends', 'babel-runtime/core-js/json/stringify', 'babel-runtime/helpers/objectWithoutProperties', 'stat/core'], factory) :
	(global.stat = factory(global._typeof,global._Promise,global._Object$assign,global._extends,global._JSON$stringify,global._objectWithoutProperties,global.Stat));
}(this, (function (_typeof,_Promise,_Object$assign,_extends,_JSON$stringify,_objectWithoutProperties,Stat) { 'use strict';

	_typeof = _typeof && _typeof.hasOwnProperty('default') ? _typeof['default'] : _typeof;
	_Promise = _Promise && _Promise.hasOwnProperty('default') ? _Promise['default'] : _Promise;
	_Object$assign = _Object$assign && _Object$assign.hasOwnProperty('default') ? _Object$assign['default'] : _Object$assign;
	_extends = _extends && _extends.hasOwnProperty('default') ? _extends['default'] : _extends;
	_JSON$stringify = _JSON$stringify && _JSON$stringify.hasOwnProperty('default') ? _JSON$stringify['default'] : _JSON$stringify;
	_objectWithoutProperties = _objectWithoutProperties && _objectWithoutProperties.hasOwnProperty('default') ? _objectWithoutProperties['default'] : _objectWithoutProperties;
	Stat = Stat && Stat.hasOwnProperty('default') ? Stat['default'] : Stat;

	var vueCore = {
		install: function install(vm) {
			var _this = this;

			var _options = this.options,
			    report = _options.report,
			    chainLength = _options.chainLength,
			    oncePageView = _options.oncePageView,
			    onceNodeView = _options.onceNodeView;


			var stat = new Stat(report, {
				chainLength: chainLength,
				oncePageView: oncePageView,
				onceNodeView: onceNodeView,
				startCallback: function startCallback(callback) {
					return _this.data.startCallback = callback;
				}
			});

			vm.prototype.$Stat = stat;
			vm.prototype.$clickStat = function (arg, opt) {
				return stat.clickReport(arg, opt);
			};
			vm.prototype.$viewStat = function (arg, opt) {
				return stat.viewReport(arg, opt);
			};
			vm.prototype.$stat = function (arg, opt) {
				return stat.nodeReport(arg, opt);
			};
			vm.prototype.$handStat = function (el, value) {
				return stat.bindDom(el, value);
			};
			vm.prototype.$removeHandStat = function (el, value) {
				return stat.removeBind(el, value);
			};
			var _bindDom = function _bindDom(el) {
				var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

				var _ref$value = _ref.value;
				_ref$value = _ref$value === undefined ? {} : _ref$value;

				var _ref$value$id = _ref$value.id,
				    id = _ref$value$id === undefined ? '' : _ref$value$id,
				    _ref$value$unBind = _ref$value.unBind,
				    unBind = _ref$value$unBind === undefined ? false : _ref$value$unBind,
				    _ref$value$unClick = _ref$value.unClick,
				    unClick = _ref$value$unClick === undefined ? false : _ref$value$unClick,
				    params = _objectWithoutProperties(_ref$value, ['id', 'unBind', 'unClick']),
				    oldValue = _ref.oldValue;

				if (!id || unBind) return;
				if (oldValue) {
					var _id = oldValue.id,
					    _unBind = oldValue.unBind,
					    _unClick = oldValue.unClick,
					    _oldValue = _objectWithoutProperties(oldValue, ['id', 'unBind', 'unClick']);

					if (_JSON$stringify(_oldValue) === _JSON$stringify(params)) return;
				}

				var key = stat._currentKey !== stat._options.DOM_toKey ? stat._options.DOM_toKey : null;

				return stat.bindDom(el, _extends({ id: id, unBind: unBind, unClick: unClick }, params), key);
			};

			var _removeDom = function _removeDom(el) {
				var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
				    _ref2$value = _ref2.value;

				_ref2$value = _ref2$value === undefined ? {} : _ref2$value;
				var _ref2$value$id = _ref2$value.id,
				    id = _ref2$value$id === undefined ? '' : _ref2$value$id,
				    _ref2$value$unBind = _ref2$value.unBind,
				    unBind = _ref2$value$unBind === undefined ? false : _ref2$value$unBind,
				    _ref2$value$unClick = _ref2$value.unClick,
				    unClick = _ref2$value$unClick === undefined ? false : _ref2$value$unClick;

				if (!id || unBind) return;

				return stat.removeBind(el, { id: id, unBind: unBind, unClick: unClick });
			};

			vm.directive('stat', {
				inserted: _bindDom,
				componentUpdated: _bindDom,
				unbind: _removeDom
			});

			stat.options.otherDataDefault = {
				pageStackMetaData: function pageStackMetaData() {
					return stat._route.getOwnPageStackParams('pageStackParams');
				},
				pageChainMetaData: function pageChainMetaData() {
					return stat._route.getOwnPageChainParams('pageChainParams');
				}
			};

			if (this.options.unRouter) {
				stat.options.baseDataDefault.page = stat._route.options.pageStackParams.statName = stat._route.options.pageChainParams.statName = this.options.statName;

				if (typeof this.data.startCallback === 'function') {
					this.data.startCallback();
					this.data.startCallback = null;
				}
			} else {
				this.startRouter(stat);
			}

			this.data.installed = true;
			this.data.stat = stat;
		},
		startRouter: function startRouter() {
			var _this2 = this;

			var stat = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.data.stat;

			typeof this.data.route === 'function' && this.data.route();

			var parentParams = {};
			var fromName = '/';
			var DOM_fromKey = '/';
			var router = this.options.router;

			this.data.route = router && (stat.options.stopRouteReport = true) && router.afterEach(function (to, from) {
				var _options2 = _this2.options,
				    metaPageName = _options2.metaPageName,
				    metaPageParamsGet = _options2.metaPageParamsGet,
				    _from = _options2.from,
				    stopVueRouteReport = _options2.stopVueRouteReport;

				var metaStat = to.meta.statParams || to.meta._stat || {};

				var h5 = _from === 'client' ? 'H5-' : '';

				var toName = stat._route.options.pageStackParams.statName = stat._route.options.pageChainParams.statName = metaStat.pageName || metaStat.page || to.meta[metaPageName] || to.meta.statName || '' + h5 + (to.meta.name || to.path);

				var params = metaStat.pageParams || (typeof to.meta[metaPageParamsGet] === 'function' ? to.meta[metaPageParamsGet](to, from) || false : false) || _Object$assign({}, to.params, to.query);

				var DOM_toKey = to.path.match(/^\/[^/]*/)[0];
				stat._options.DOM_toKey = DOM_toKey;
				stat._options.DOM_fromKey = DOM_fromKey;

				stat.options.baseDataDefault = {
					page: toName,
					parentPage: fromName,
					parentParams: parentParams,
					pageParams: params,
					actionParams: metaStat.actionParams || {}
				};

				fromName = toName;
				DOM_fromKey = DOM_toKey;
				parentParams = params;

				stat.options.reportPathname = to.fullPath;

				if (typeof _this2.data.startCallback === 'function') {
					_this2.data.startCallback();
					_this2.data.startCallback = null;
				}

				if (to.meta._autoStat === false) return;

				if (stopVueRouteReport) return;

				setTimeout(function () {
					return stat.viewReport();
				}, 4);
			});
		},
		set: function set(key, value) {
			var _this3 = this;

			if (!this.options.router && (key === 'router' || key.router) && this.data.installed) _Promise.resolve().then(function () {
				return _this3.startRouter();
			});

			if ((typeof key === 'undefined' ? 'undefined' : _typeof(key)) === 'object') return _Object$assign(this.options, key);

			return this.options[key] = value;
		},

		options: {
			from: '',

			router: null,

			unRouter: false,

			statName: document.title,

			report: null,

			metaPageName: '',

			metaPageParamsGet: '',
			chainLength: 6,
			oncePageView: true,
			onceNodeView: true,

			stopVueRouteReport: false
		},
		data: {
			route: null,

			installed: false,

			stat: null,

			startCallback: null
		}
	};

	return vueCore;

})));
